<?php
namespace Admin\Controller;
use Think\Controller;
use User\Api\UserApi;

class LoginController extends Controller {

    /**
     * 用户登录，包括本地用户和第三方用户
     * @param type $username 用户名
     * @param type $password 密码
     * @param type $verify 验证码
     * @param type $remember 是否记住密码
     * @param type $is_third 是否是第三方登录
     */
    public function login($username="", $password="", $verify="", $remember=0, $is_third=1){
        if(IS_POST){
//             if(!check_verify($verify)){
//                 $data = array(
//                     'code' => -5,
//                     'msg'  => '验证码错误',
//                 );
//                 $this->ajaxReturn($data);
//             }
            $userapi = new UserApi();
            $user_third = $userapi->login($username, $password, $remember, $is_third);

            if($user_third['id'] > 0){
                if($is_third){
                    $this->third2Local($user_third, $username);   //第三方登录时对本地操作
                    $data = array(
                        'code' => 2,
                        'msg'  => '老A用户登录成功',
                    );
                }else{
                    $data = array(
                         'code' => 1,
                         'msg'  => '本地登录成功',
                    );
                }
                $this->ajaxReturn($data);
            }else{
                switch ($user['id']){
                    case -1: 
                        $msg = '用户名不存在！';
                        break;
                    case -2:
                        $msg = '用户名密码不匹配！';
                        break;
                    case -3:
                        $msg = $user['msg'];
                        break;
                    case -4:
                        $msg = '该用户被禁用！';
                        break;;
                    default :
                        $uid = -6;
                        $msg = '登录不成功，请重新登录！';
                }
                $this->ajaxReturn(array(
                    'code' => $uid,
                    'msg'  => $msg,
                ));
            }
        }else{
            if(is_login()){
                $data = array(
                    'code' => 0,
                    'msg'  => '用户已经登录',
                );
                $this->ajaxReturn($data);
            }
            $this->display();
        }
    }

    /**
     * 第三方登录对本地操作
     * 包括对本地用户表操作，商家表操作，以及保存用户登录信息     * 
     */
    public function third2Local($user_third, $username){

        //判断该第三方用户是否存在本地数据库
        if($user = M('User')->field('id')->where(array('third_id' => $user_third['id']))->find()){
            $uid = $user['id'];
            M('User')->save(array('last_login_time' => NOW_TIME, 'id' => $uid)); //更新本地数据库登录信息
            //更新卖家信息
            M('User_seller')->where(array('uid' => $uid))->save(
                    array(
                        'rank'      => $user_third['rank'], 
                        'user_type' => $user['user_type']
                    ));
        }else{
            //在本地数据库中新增用户
            $data = array(
                'third_id' => $user_third['id'],
                'last_login_time' => NOW_TIME,
                'reg_time' => NOW_TIME,
                'nickname' => $username,
                'username' => 'laoA_'.$username,
                'realname' => $user_third['realname'],
                'usertype' => 1,
            );
            $uid = M('User')->add($data); //在本地用户表添加数据
            
            //新增卖家用户
            $seller = array(
                'uid'       => $uid, 
                'nickname'  => $username,
                'rank'      => $user_third['rank'],
                'user_type' => $user_third['user_type'],
            );
            M('User_seller')->add($seller);

            //卖家基本权限设定,暂时写死设定权限：2
            M('Auth_group_access')->add(array('uid' => $uid, 'group_id' => 2));
        }

        //保存用户登录信息
        $userinfo = array('id' => $uid,
            'username' => $username);
        session('userinfo', $userinfo);    
    }
    
    /**
     * 退出
     */
    public function logout(){
        session('userinfo', NULL);
        $this->redirect('Admin/login/login');
    } 
    
    /**
     * 验证码
     */
    public function verify(){
        $verify = new \Think\Verify();
        $verify->entry(1);
    }
}
